home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Codigo / hh / rsource.exe / Hexen Source / P_SETUP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-22  |  26.6 KB  |  1,219 lines

  1.  
  2. //**************************************************************************
  3. //**
  4. //** p_setup.c : Heretic 2 : Raven Software, Corp.
  5. //**
  6. //** $RCSfile: p_setup.c,v $
  7. //** $Revision: 1.39 $
  8. //** $Date: 96/03/12 12:04:11 $
  9. //** $Author: bgokey $
  10. //**
  11. //**************************************************************************
  12.  
  13. // HEADER FILES ------------------------------------------------------------
  14.  
  15. #include <math.h>
  16. #include <stdlib.h>
  17. #include "h2def.h"
  18. #include "p_local.h"
  19. #include "soundst.h"
  20.  
  21. // MACROS ------------------------------------------------------------------
  22.  
  23. #define MAPINFO_SCRIPT_NAME "MAPINFO"
  24. #define MCMD_SKY1 1
  25. #define MCMD_SKY2 2
  26. #define MCMD_LIGHTNING 3
  27. #define MCMD_FADETABLE 4
  28. #define MCMD_DOUBLESKY 5
  29. #define MCMD_CLUSTER 6
  30. #define MCMD_WARPTRANS 7
  31. #define MCMD_NEXT 8
  32. #define MCMD_CDTRACK 9
  33. #define MCMD_CD_STARTTRACK 10
  34. #define MCMD_CD_END1TRACK 11
  35. #define MCMD_CD_END2TRACK 12
  36. #define MCMD_CD_END3TRACK 13
  37. #define MCMD_CD_INTERTRACK 14
  38. #define MCMD_CD_TITLETRACK 15
  39.  
  40. #define UNKNOWN_MAP_NAME "DEVELOPMENT MAP"
  41. #define DEFAULT_SKY_NAME "SKY1"
  42. #define DEFAULT_SONG_LUMP "DEFSONG"
  43. #define DEFAULT_FADE_TABLE "COLORMAP"
  44.  
  45. // TYPES -------------------------------------------------------------------
  46.  
  47. typedef struct mapInfo_s mapInfo_t;
  48. struct mapInfo_s
  49. {
  50.     short cluster;
  51.     short warpTrans;
  52.     short nextMap;
  53.     short cdTrack;
  54.     char name[32];
  55.     short sky1Texture;
  56.     short sky2Texture;
  57.     fixed_t sky1ScrollDelta;
  58.     fixed_t sky2ScrollDelta;
  59.     boolean doubleSky;
  60.     boolean lightning;
  61.     int fadetable;
  62.     char songLump[10];
  63. };
  64.  
  65. // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
  66.  
  67. void P_SpawnMapThing(mapthing_t *mthing);
  68.  
  69. // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
  70.  
  71. // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
  72.  
  73. static int QualifyMap(int map);
  74.  
  75. // EXTERNAL DATA DECLARATIONS ----------------------------------------------
  76.  
  77. // PUBLIC DATA DEFINITIONS -------------------------------------------------
  78.  
  79. int MapCount;
  80. mapthing_t deathmatchstarts[MAXDEATHMATCHSTARTS], *deathmatch_p;
  81. mapthing_t playerstarts[MAX_PLAYER_STARTS][MAXPLAYERS];
  82. int numvertexes;
  83. vertex_t *vertexes;
  84. int numsegs;
  85. seg_t *segs;
  86. int numsectors;
  87. sector_t *sectors;
  88. int numsubsectors;
  89. subsector_t *subsectors;
  90. int numnodes;
  91. node_t *nodes;
  92. int numlines;
  93. line_t *lines;
  94. int numsides;
  95. side_t *sides;
  96. short *blockmaplump; // offsets in blockmap are from here
  97. short *blockmap;
  98. int bmapwidth, bmapheight; // in mapblocks
  99. fixed_t bmaporgx, bmaporgy; // origin of block map
  100. mobj_t **blocklinks; // for thing chains
  101. byte *rejectmatrix; // for fast sight rejection
  102.  
  103. // PRIVATE DATA DEFINITIONS ------------------------------------------------
  104.  
  105. static mapInfo_t MapInfo[99];
  106. static char *MapCmdNames[] =
  107. {
  108.     "SKY1",
  109.     "SKY2",
  110.     "DOUBLESKY",
  111.     "LIGHTNING",
  112.     "FADETABLE",
  113.     "CLUSTER",
  114.     "WARPTRANS",
  115.     "NEXT",
  116.     "CDTRACK",
  117.     "CD_START_TRACK",
  118.     "CD_END1_TRACK",
  119.     "CD_END2_TRACK",
  120.     "CD_END3_TRACK",
  121.     "CD_INTERMISSION_TRACK",
  122.     "CD_TITLE_TRACK",
  123.     NULL
  124. };
  125. static int MapCmdIDs[] =
  126. {
  127.     MCMD_SKY1,
  128.     MCMD_SKY2,
  129.     MCMD_DOUBLESKY,
  130.     MCMD_LIGHTNING,
  131.     MCMD_FADETABLE,
  132.     MCMD_CLUSTER,
  133.     MCMD_WARPTRANS,
  134.     MCMD_NEXT,
  135.     MCMD_CDTRACK,
  136.     MCMD_CD_STARTTRACK,
  137.     MCMD_CD_END1TRACK,
  138.     MCMD_CD_END2TRACK,
  139.     MCMD_CD_END3TRACK,
  140.     MCMD_CD_INTERTRACK,
  141.     MCMD_CD_TITLETRACK
  142. };
  143.  
  144. static int cd_NonLevelTracks[6]; // Non-level specific song cd track numbers 
  145.  
  146. // CODE --------------------------------------------------------------------
  147.  
  148. /*
  149. =================
  150. =
  151. = P_LoadVertexes
  152. =
  153. =================
  154. */
  155.  
  156. void P_LoadVertexes (int lump)
  157. {
  158.     byte            *data;
  159.     int                     i;
  160.     mapvertex_t     *ml;
  161.     vertex_t        *li;
  162.  
  163.     numvertexes = W_LumpLength (lump) / sizeof(mapvertex_t);
  164.     vertexes = Z_Malloc (numvertexes*sizeof(vertex_t),PU_LEVEL,0);
  165.     data = W_CacheLumpNum (lump,PU_STATIC);
  166.  
  167.     ml = (mapvertex_t *)data;
  168.     li = vertexes;
  169.     for (i=0 ; i<numvertexes ; i++, li++, ml++)
  170.     {
  171.         li->x = SHORT(ml->x)<<FRACBITS;
  172.         li->y = SHORT(ml->y)<<FRACBITS;
  173.     }
  174.  
  175.     Z_Free (data);
  176. }
  177.  
  178.  
  179. /*
  180. =================
  181. =
  182. = P_LoadSegs
  183. =
  184. =================
  185. */
  186.  
  187. void P_LoadSegs (int lump)
  188. {
  189.     byte            *data;
  190.     int                     i;
  191.     mapseg_t        *ml;
  192.     seg_t           *li;
  193.     line_t  *ldef;
  194.     int                     linedef, side;
  195.  
  196.     numsegs = W_LumpLength (lump) / sizeof(mapseg_t);
  197.     segs = Z_Malloc (numsegs*sizeof(seg_t),PU_LEVEL,0);
  198.     memset (segs, 0, numsegs*sizeof(seg_t));
  199.     data = W_CacheLumpNum (lump,PU_STATIC);
  200.  
  201.     ml = (mapseg_t *)data;
  202.     li = segs;
  203.     for (i=0 ; i<numsegs ; i++, li++, ml++)
  204.     {
  205.         li->v1 = &vertexes[SHORT(ml->v1)];
  206.         li->v2 = &vertexes[SHORT(ml->v2)];
  207.  
  208.         li->angle = (SHORT(ml->angle))<<16;
  209.         li->offset = (SHORT(ml->offset))<<16;
  210.         linedef = SHORT(ml->linedef);
  211.         ldef = &lines[linedef];
  212.         li->linedef = ldef;
  213.         side = SHORT(ml->side);
  214.         li->sidedef = &sides[ldef->sidenum[side]];
  215.         li->frontsector = sides[ldef->sidenum[side]].sector;
  216.         if (ldef-> flags & ML_TWOSIDED)
  217.             li->backsector = sides[ldef->sidenum[side^1]].sector;
  218.         else
  219.             li->backsector = 0;
  220.     }
  221.  
  222.     Z_Free (data);
  223. }
  224.  
  225.  
  226. /*
  227. =================
  228. =
  229. = P_LoadSubsectors
  230. =
  231. =================
  232. */
  233.  
  234. void P_LoadSubsectors (int lump)
  235. {
  236.     byte                    *data;
  237.     int                             i;
  238.     mapsubsector_t  *ms;
  239.     subsector_t             *ss;
  240.  
  241.     numsubsectors = W_LumpLength (lump) / sizeof(mapsubsector_t);
  242.     subsectors = Z_Malloc (numsubsectors*sizeof(subsector_t),PU_LEVEL,0);
  243.     data = W_CacheLumpNum (lump,PU_STATIC);
  244.  
  245.     ms = (mapsubsector_t *)data;
  246.     memset (subsectors,0, numsubsectors*sizeof(subsector_t));
  247.     ss = subsectors;
  248.     for (i=0 ; i<numsubsectors ; i++, ss++, ms++)
  249.     {
  250.         ss->numlines = SHORT(ms->numsegs);
  251.         ss->firstline = SHORT(ms->firstseg);
  252.     }
  253.  
  254.     Z_Free (data);
  255. }
  256.  
  257.  
  258. /*
  259. =================
  260. =
  261. = P_LoadSectors
  262. =
  263. =================
  264. */
  265.  
  266. void P_LoadSectors (int lump)
  267. {
  268.     byte                    *data;
  269.     int                             i;
  270.     mapsector_t             *ms;
  271.     sector_t                *ss;
  272.  
  273.     numsectors = W_LumpLength (lump) / sizeof(mapsector_t);
  274.     sectors = Z_Malloc (numsectors*sizeof(sector_t),PU_LEVEL,0);
  275.     memset (sectors, 0, numsectors*sizeof(sector_t));
  276.     data = W_CacheLumpNum (lump,PU_STATIC);
  277.  
  278.     ms = (mapsector_t *)data;
  279.     ss = sectors;
  280.  
  281.     // Make sure primary lumps are used for flat searching
  282.     W_UsePrimary();
  283.  
  284.     for(i = 0; i < numsectors; i++, ss++, ms++)
  285.     {
  286.         ss->floorheight = SHORT(ms->floorheight)<<FRACBITS;
  287.         ss->ceilingheight = SHORT(ms->ceilingheight)<<FRACBITS;
  288.         ss->floorpic = R_FlatNumForName(ms->floorpic);
  289.         ss->ceilingpic = R_FlatNumForName(ms->ceilingpic);
  290.         ss->lightlevel = SHORT(ms->lightlevel);
  291.         ss->special = SHORT(ms->special);
  292.         ss->tag = SHORT(ms->tag);
  293.         ss->thinglist = NULL;
  294.         ss->seqType = SEQTYPE_STONE; // default seqType
  295.     }
  296.     if(DevMaps)
  297.     {
  298.         W_UseAuxiliary();
  299.     }
  300.     Z_Free(data);
  301. }
  302.  
  303.  
  304. /*
  305. =================
  306. =
  307. = P_LoadNodes
  308. =
  309. =================
  310. */
  311.  
  312. void P_LoadNodes (int lump)
  313. {
  314.     byte            *data;
  315.     int                     i,j,k;
  316.     mapnode_t       *mn;
  317.     node_t          *no;
  318.  
  319.     numnodes = W_LumpLength (lump) / sizeof(mapnode_t);
  320.     nodes = Z_Malloc (numnodes*sizeof(node_t),PU_LEVEL,0);
  321.     data = W_CacheLumpNum (lump,PU_STATIC);
  322.  
  323.     mn = (mapnode_t *)data;
  324.     no = nodes;
  325.     for (i=0 ; i<numnodes ; i++, no++, mn++)
  326.     {
  327.         no->x = SHORT(mn->x)<<FRACBITS;
  328.         no->y = SHORT(mn->y)<<FRACBITS;
  329.         no->dx = SHORT(mn->dx)<<FRACBITS;
  330.         no->dy = SHORT(mn->dy)<<FRACBITS;
  331.         for (j=0 ; j<2 ; j++)
  332.         {
  333.             no->children[j] = SHORT(mn->children[j]);
  334.             for (k=0 ; k<4 ; k++)
  335.                 no->bbox[j][k] = SHORT(mn->bbox[j][k])<<FRACBITS;
  336.         }
  337.     }
  338.     Z_Free (data);
  339. }
  340.  
  341. //==========================================================================
  342. //
  343. // P_LoadThings
  344. //
  345. //==========================================================================
  346.  
  347. void P_LoadThings(int lump)
  348. {
  349.     byte *data;
  350.     int i;
  351.     mapthing_t *mt;
  352.     int numthings;
  353.     int playerCount;
  354.     int deathSpotsCount;
  355.  
  356.     data = W_CacheLumpNum(lump, PU_STATIC);
  357.     numthings = W_LumpLength(lump)/sizeof(mapthing_t);
  358.  
  359.     mt = (mapthing_t *)data;
  360.     for(i = 0; i < numthings; i++, mt++)
  361.     {
  362.         mt->tid = SHORT(mt->tid);
  363.         mt->x = SHORT(mt->x);
  364.         mt->y = SHORT(mt->y);
  365.         mt->height = SHORT(mt->height);
  366.         mt->angle = SHORT(mt->angle);
  367.         mt->type = SHORT(mt->type);
  368.         mt->options = SHORT(mt->options);
  369.         P_SpawnMapThing(mt);
  370.     }
  371.     P_CreateTIDList();
  372.     P_InitCreatureCorpseQueue(false); // false = do NOT scan for corpses
  373.     Z_Free(data);
  374.  
  375.     if(!deathmatch)
  376.     { // Don't need to check deathmatch spots
  377.         return;
  378.     }
  379.     playerCount = 0;
  380.     for(i = 0; i < MAXPLAYERS; i++)
  381.     {
  382.         playerCount += playeringame[i];
  383.     }
  384.     deathSpotsCount = deathmatch_p-deathmatchstarts;
  385.     if(deathSpotsCount < playerCount)
  386.     {
  387.         I_Error("P_LoadThings: Player count (%d) exceeds deathmatch "
  388.             "spots (%d)", playerCount, deathSpotsCount);
  389.     }
  390. }
  391.  
  392. /*
  393. =================
  394. =
  395. = P_LoadLineDefs
  396. =
  397. =================
  398. */
  399.  
  400. void P_LoadLineDefs(int lump)
  401. {
  402.     byte *data;
  403.     int i;
  404.     maplinedef_t *mld;
  405.     line_t *ld;
  406.     vertex_t *v1, *v2;
  407.  
  408.     numlines = W_LumpLength(lump)/sizeof(maplinedef_t);
  409.     lines = Z_Malloc(numlines*sizeof(line_t), PU_LEVEL, 0);
  410.     memset(lines, 0, numlines*sizeof(line_t));
  411.     data = W_CacheLumpNum(lump, PU_STATIC);
  412.  
  413.     mld = (maplinedef_t *)data;
  414.     ld = lines;
  415.     for(i = 0; i < numlines; i++, mld++, ld++)
  416.     {
  417.         ld->flags = SHORT(mld->flags);
  418.  
  419.         // Old line special info ...
  420.         //ld->special = SHORT(mld->special);
  421.         //ld->tag = SHORT(mld->tag);
  422.  
  423.         // New line special info ...
  424.         ld->special = mld->special;
  425.         ld->arg1 = mld->arg1;
  426.         ld->arg2 = mld->arg2;
  427.         ld->arg3 = mld->arg3;
  428.         ld->arg4 = mld->arg4;
  429.         ld->arg5 = mld->arg5;
  430.  
  431.         v1 = ld->v1 = &vertexes[SHORT(mld->v1)];
  432.         v2 = ld->v2 = &vertexes[SHORT(mld->v2)];
  433.         ld->dx = v2->x - v1->x;
  434.         ld->dy = v2->y - v1->y;
  435.         if (!ld->dx)
  436.             ld->slopetype = ST_VERTICAL;
  437.         else if (!ld->dy)
  438.             ld->slopetype = ST_HORIZONTAL;
  439.         else
  440.         {
  441.             if (FixedDiv (ld->dy , ld->dx) > 0)
  442.                 ld->slopetype = ST_POSITIVE;
  443.             else
  444.                 ld->slopetype = ST_NEGATIVE;
  445.         }
  446.  
  447.         if (v1->x < v2->x)
  448.         {
  449.             ld->bbox[BOXLEFT] = v1->x;
  450.             ld->bbox[BOXRIGHT] = v2->x;
  451.         }
  452.         else
  453.         {
  454.             ld->bbox[BOXLEFT] = v2->x;
  455.             ld->bbox[BOXRIGHT] = v1->x;
  456.         }
  457.         if (v1->y < v2->y)
  458.         {
  459.             ld->bbox[BOXBOTTOM] = v1->y;
  460.             ld->bbox[BOXTOP] = v2->y;
  461.         }
  462.         else
  463.         {
  464.             ld->bbox[BOXBOTTOM] = v2->y;
  465.             ld->bbox[BOXTOP] = v1->y;
  466.         }
  467.         ld->sidenum[0] = SHORT(mld->sidenum[0]);
  468.         ld->sidenum[1] = SHORT(mld->sidenum[1]);
  469.         if (ld->sidenum[0] != -1)
  470.             ld->frontsector = sides[ld->sidenum[0]].sector;
  471.         else
  472.             ld->frontsector = 0;
  473.         if (ld->sidenum[1] != -1)
  474.             ld->backsector = sides[ld->sidenum[1]].sector;
  475.         else
  476.             ld->backsector = 0;
  477.     }
  478.  
  479.     Z_Free (data);
  480. }
  481.  
  482.  
  483. /*
  484. =================
  485. =
  486. = P_LoadSideDefs
  487. =
  488. =================
  489. */
  490.  
  491. void P_LoadSideDefs (int lump)
  492. {
  493.     byte                    *data;
  494.     int                             i;
  495.     mapsidedef_t    *msd;
  496.     side_t                  *sd;
  497.  
  498.     numsides = W_LumpLength (lump) / sizeof(mapsidedef_t);
  499.     sides = Z_Malloc (numsides*sizeof(side_t),PU_LEVEL,0);
  500.     memset (sides, 0, numsides*sizeof(side_t));
  501.     data = W_CacheLumpNum (lump,PU_STATIC);
  502.  
  503.     msd = (mapsidedef_t *)data;
  504.     sd = sides;
  505.  
  506.     // Make sure primary lumps are used for texture searching
  507.     W_UsePrimary();
  508.  
  509.     for(i = 0; i < numsides; i++, msd++, sd++)
  510.     {
  511.         sd->textureoffset = SHORT(msd->textureoffset)<<FRACBITS;
  512.         sd->rowoffset = SHORT(msd->rowoffset)<<FRACBITS;
  513.         sd->toptexture = R_TextureNumForName(msd->toptexture);
  514.         sd->bottomtexture = R_TextureNumForName(msd->bottomtexture);
  515.         sd->midtexture = R_TextureNumForName(msd->midtexture);
  516.         sd->sector = §ors[SHORT(msd->sector)];
  517.     }
  518.     if(DevMaps)
  519.     {
  520.         W_UseAuxiliary();
  521.     }
  522.     Z_Free(data);
  523. }
  524.  
  525. /*
  526. =================
  527. =
  528. = P_LoadBlockMap
  529. =
  530. =================
  531. */
  532.  
  533. void P_LoadBlockMap (int lump)
  534. {
  535.     int             i, count;
  536.  
  537.     blockmaplump = W_CacheLumpNum (lump,PU_LEVEL);
  538.     blockmap = blockmaplump+4;
  539.     count = W_LumpLength (lump)/2;
  540.     for (i=0 ; i<count ; i++)
  541.         blockmaplump[i] = SHORT(blockmaplump[i]);
  542.  
  543.     bmaporgx = blockmaplump[0]<<FRACBITS;
  544.     bmaporgy = blockmaplump[1]<<FRACBITS;
  545.     bmapwidth = blockmaplump[2];
  546.     bmapheight = blockmaplump[3];
  547.  
  548. // clear out mobj chains
  549.     count = sizeof(*blocklinks)* bmapwidth*bmapheight;
  550.     blocklinks = Z_Malloc (count,PU_LEVEL, 0);
  551.     memset (blocklinks, 0, count);
  552. }
  553.  
  554.  
  555.  
  556.  
  557. /*
  558. =================
  559. =
  560. = P_GroupLines
  561. =
  562. = Builds sector line lists and subsector sector numbers
  563. = Finds block bounding boxes for sectors
  564. =================
  565. */
  566.  
  567. void P_GroupLines (void)
  568. {
  569.     line_t          **linebuffer;
  570.     int                     i, j, total;
  571.     line_t          *li;
  572.     sector_t        *sector;
  573.     subsector_t     *ss;
  574.     seg_t           *seg;
  575.     fixed_t         bbox[4];
  576.     int                     block;
  577.  
  578. // look up sector number for each subsector
  579.     ss = subsectors;
  580.     for (i=0 ; i<numsubsectors ; i++, ss++)
  581.     {
  582.         seg = &segs[ss->firstline];
  583.         ss->sector = seg->sidedef->sector;
  584.     }
  585.  
  586. // count number of lines in each sector
  587.     li = lines;
  588.     total = 0;
  589.     for (i=0 ; i<numlines ; i++, li++)
  590.     {
  591.         total++;
  592.         li->frontsector->linecount++;
  593.         if (li->backsector && li->backsector != li->frontsector)
  594.         {
  595.             li->backsector->linecount++;
  596.             total++;
  597.         }
  598.     }
  599.  
  600. // build line tables for each sector
  601.     linebuffer = Z_Malloc (total*4, PU_LEVEL, 0);
  602.     sector = sectors;
  603.     for (i=0 ; i<numsectors ; i++, sector++)
  604.     {
  605.         M_ClearBox (bbox);
  606.         sector->lines = linebuffer;
  607.         li = lines;
  608.         for (j=0 ; j<numlines ; j++, li++)
  609.         {
  610.             if (li->frontsector == sector || li->backsector == sector)
  611.             {
  612.                 *linebuffer++ = li;
  613.                 M_AddToBox (bbox, li->v1->x, li->v1->y);
  614.                 M_AddToBox (bbox, li->v2->x, li->v2->y);
  615.             }
  616.         }
  617.         if (linebuffer - sector->lines != sector->linecount)
  618.             I_Error ("P_GroupLines: miscounted");
  619.  
  620.         // set the degenmobj_t to the middle of the bounding box
  621.         sector->soundorg.x = (bbox[BOXRIGHT]+bbox[BOXLEFT])/2;
  622.         sector->soundorg.y = (bbox[BOXTOP]+bbox[BOXBOTTOM])/2;
  623.  
  624.         // adjust bounding box to map blocks
  625.         block = (bbox[BOXTOP]-bmaporgy+MAXRADIUS)>>MAPBLOCKSHIFT;
  626.         block = block >= bmapheight ? bmapheight-1 : block;
  627.         sector->blockbox[BOXTOP]=block;
  628.  
  629.         block = (bbox[BOXBOTTOM]-bmaporgy-MAXRADIUS)>>MAPBLOCKSHIFT;
  630.         block = block < 0 ? 0 : block;
  631.         sector->blockbox[BOXBOTTOM]=block;
  632.  
  633.         block = (bbox[BOXRIGHT]-bmaporgx+MAXRADIUS)>>MAPBLOCKSHIFT;
  634.         block = block >= bmapwidth ? bmapwidth-1 : block;
  635.         sector->blockbox[BOXRIGHT]=block;
  636.  
  637.         block = (bbox[BOXLEFT]-bmaporgx-MAXRADIUS)>>MAPBLOCKSHIFT;
  638.         block = block < 0 ? 0 : block;
  639.         sector->blockbox[BOXLEFT]=block;
  640.     }
  641.  
  642. }
  643.  
  644. //=============================================================================
  645.  
  646.  
  647. /*
  648. =================
  649. =
  650. = P_SetupLevel
  651. =
  652. =================
  653. */
  654.  
  655. #ifdef __WATCOMC__
  656.     extern boolean i_CDMusic;
  657. #endif
  658.  
  659. void P_SetupLevel(int episode, int map, int playermask, skill_t skill)
  660. {
  661.     int i;
  662.     int parm;
  663.     char lumpname[9];
  664.     char auxName[128];
  665.     int lumpnum;
  666.     mobj_t *mobj;
  667.  
  668.     for(i = 0; i < MAXPLAYERS; i++)
  669.     {
  670.         players[i].killcount = players[i].secretcount
  671.             = players[i].itemcount = 0;
  672.     }
  673.     players[consoleplayer].viewz = 1; // will be set by player think
  674.  
  675. #ifdef __WATCOMC__
  676.     if(i_CDMusic == false)
  677.     {
  678.         S_StartSongName("chess", true); // Waiting-for-level-load song
  679.     }
  680. #endif
  681.  
  682.     Z_FreeTags(PU_LEVEL, PU_PURGELEVEL-1);
  683.  
  684.     P_InitThinkers();
  685.     leveltime = 0;
  686.  
  687.     if(DevMaps)
  688.     {
  689.         sprintf(auxName, "%sMAP%02d.WAD", DevMapsDir, map);
  690.         W_OpenAuxiliary(auxName);
  691.     }
  692.     sprintf(lumpname, "MAP%02d", map);
  693.     lumpnum = W_GetNumForName(lumpname);
  694.     //
  695.     // Begin processing map lumps
  696.     // Note: most of this ordering is important
  697.     //
  698.     P_LoadBlockMap(lumpnum+ML_BLOCKMAP);
  699.     P_LoadVertexes(lumpnum+ML_VERTEXES);
  700.     P_LoadSectors(lumpnum+ML_SECTORS);
  701.     P_LoadSideDefs(lumpnum+ML_SIDEDEFS);
  702.     P_LoadLineDefs(lumpnum+ML_LINEDEFS);
  703.     P_LoadSubsectors(lumpnum+ML_SSECTORS);
  704.     P_LoadNodes(lumpnum+ML_NODES);
  705.     P_LoadSegs(lumpnum+ML_SEGS);
  706.     rejectmatrix = W_CacheLumpNum(lumpnum+ML_REJECT, PU_LEVEL);
  707.     P_GroupLines();
  708.     bodyqueslot = 0;
  709.     po_NumPolyobjs = 0;
  710.     deathmatch_p = deathmatchstarts;
  711.     P_LoadThings(lumpnum+ML_THINGS);
  712.     PO_Init(lumpnum+ML_THINGS); // Initialize the polyobjs
  713.     P_LoadACScripts(lumpnum+ML_BEHAVIOR); // ACS object code
  714.     //
  715.     // End of map lump processing
  716.     //
  717.     if(DevMaps)
  718.     {
  719.         // Close the auxiliary file, but don't free its loaded lumps.
  720.         // The next call to W_OpenAuxiliary() will do a full shutdown
  721.         // of the current auxiliary WAD (free lumps and info lists).
  722.         W_CloseAuxiliaryFile();
  723.         W_UsePrimary();
  724.     }
  725.  
  726.     // If deathmatch, randomly spawn the active players
  727.     TimerGame = 0;
  728.     if(deathmatch)
  729.     {
  730.         for (i=0 ; i<MAXPLAYERS ; i++)
  731.         {
  732.             if (playeringame[i])
  733.             {   // must give a player spot before deathmatchspawn
  734.                 mobj = P_SpawnMobj (playerstarts[0][i].x<<16,
  735.                     playerstarts[0][i].y<<16,0, MT_PLAYER_FIGHTER);
  736.                 players[i].mo = mobj;
  737.                 G_DeathMatchSpawnPlayer (i);
  738.                 P_RemoveMobj (mobj);
  739.             }
  740.         }
  741.         parm = M_CheckParm("-timer");
  742.         if(parm && parm < myargc-1)
  743.         {
  744.             TimerGame = atoi(myargv[parm+1])*35*60;
  745.         }
  746.     }
  747.  
  748. // set up world state
  749.     P_SpawnSpecials ();
  750.  
  751. // build subsector connect matrix
  752. //      P_ConnectSubsectors ();
  753.  
  754. // Load colormap and set the fullbright flag
  755.     i = P_GetMapFadeTable(gamemap);
  756.     W_ReadLump(i, colormaps);
  757.     if(i == W_GetNumForName("COLORMAP"))
  758.     {
  759.         LevelUseFullBright = true;
  760.     }
  761.     else
  762.     { // Probably fog ... don't use fullbright sprites
  763.         LevelUseFullBright = false;
  764.     }
  765.  
  766. // preload graphics
  767.     if (precache)
  768.         R_PrecacheLevel ();
  769.  
  770.     // Check if the level is a lightning level
  771.     P_InitLightning();
  772.  
  773.     S_StopAllSound();
  774.     SN_StopAllSequences();
  775.     S_StartSong(gamemap, true);
  776.  
  777. //printf ("free memory: 0x%x\n", Z_FreeMemory());
  778.  
  779. }
  780.  
  781. //==========================================================================
  782. //
  783. // InitMapInfo
  784. //
  785. //==========================================================================
  786.  
  787. static void InitMapInfo(void)
  788. {
  789.     int map;
  790.     int mapMax;
  791.     int mcmdValue;
  792.     mapInfo_t *info;
  793.     char songMulch[10];
  794.  
  795.     mapMax = 1;
  796.  
  797.     // Put defaults into MapInfo[0]
  798.     info = MapInfo;
  799.     info->cluster = 0;
  800.     info->warpTrans = 0;
  801.     info->nextMap = 1; // Always go to map 1 if not specified
  802.     info->cdTrack = 1;
  803.     info->sky1Texture = R_TextureNumForName(DEFAULT_SKY_NAME);
  804.     info->sky2Texture = info->sky1Texture;
  805.     info->sky1ScrollDelta = 0;
  806.     info->sky2ScrollDelta = 0;
  807.     info->doubleSky = false;
  808.     info->lightning = false;
  809.     info->fadetable = W_GetNumForName(DEFAULT_FADE_TABLE);
  810.     strcpy(info->name, UNKNOWN_MAP_NAME);
  811.  
  812. //    strcpy(info->songLump, DEFAULT_SONG_LUMP);
  813.     SC_Open(MAPINFO_SCRIPT_NAME);
  814.     while(SC_GetString())
  815.     {
  816.         if(SC_Compare("MAP") == false)
  817.         {
  818.             SC_ScriptError(NULL);
  819.         }
  820.         SC_MustGetNumber();
  821.         if(sc_Number < 1 || sc_Number > 99)
  822.         { // 
  823.             SC_ScriptError(NULL);
  824.         }
  825.         map = sc_Number;
  826.  
  827.         info = &MapInfo[map];
  828.  
  829.         // Save song lump name
  830.         strcpy(songMulch, info->songLump);
  831.  
  832.         // Copy defaults to current map definition
  833.         memcpy(info, &MapInfo[0], sizeof(*info));
  834.  
  835.         // Restore song lump name
  836.         strcpy(info->songLump, songMulch);
  837.  
  838.         // The warp translation defaults to the map number
  839.         info->warpTrans = map;
  840.  
  841.         // Map name must follow the number
  842.         SC_MustGetString();
  843.         strcpy(info->name, sc_String);
  844.  
  845.         // Process optional tokens
  846.         while(SC_GetString())
  847.         {
  848.             if(SC_Compare("MAP"))
  849.             { // Start next map definition
  850.                 SC_UnGet();
  851.                 break;
  852.             }
  853.             mcmdValue = MapCmdIDs[SC_MustMatchString(MapCmdNames)];
  854.             switch(mcmdValue)
  855.             {
  856.                 case MCMD_CLUSTER:
  857.                     SC_MustGetNumber();
  858.                     info->cluster = sc_Number;
  859.                     break;
  860.                 case MCMD_WARPTRANS:
  861.                     SC_MustGetNumber();
  862.                     info->warpTrans = sc_Number;
  863.                     break;
  864.                 case MCMD_NEXT:
  865.                     SC_MustGetNumber();
  866.                     info->nextMap = sc_Number;
  867.                     break;
  868.                 case MCMD_CDTRACK:
  869.                     SC_MustGetNumber();
  870.                     info->cdTrack = sc_Number;
  871.                     break;
  872.                 case MCMD_SKY1:
  873.                     SC_MustGetString();
  874.                     info->sky1Texture = R_TextureNumForName(sc_String);
  875.                     SC_MustGetNumber();
  876.                     info->sky1ScrollDelta = sc_Number<<8;
  877.                     break;
  878.                 case MCMD_SKY2:
  879.                     SC_MustGetString();
  880.                     info->sky2Texture = R_TextureNumForName(sc_String);
  881.                     SC_MustGetNumber();
  882.                     info->sky2ScrollDelta = sc_Number<<8;
  883.                     break;
  884.                 case MCMD_DOUBLESKY:
  885.                     info->doubleSky = true;
  886.                     break;
  887.                 case MCMD_LIGHTNING:
  888.                     info->lightning = true;
  889.                     break;
  890.                 case MCMD_FADETABLE:
  891.                     SC_MustGetString();
  892.                     info->fadetable = W_GetNumForName(sc_String);
  893.                     break;
  894.                 case MCMD_CD_STARTTRACK:
  895.                 case MCMD_CD_END1TRACK:
  896.                 case MCMD_CD_END2TRACK:
  897.                 case MCMD_CD_END3TRACK:
  898.                 case MCMD_CD_INTERTRACK:
  899.                 case MCMD_CD_TITLETRACK:
  900.                     SC_MustGetNumber();
  901.                     cd_NonLevelTracks[mcmdValue-MCMD_CD_STARTTRACK] = 
  902.                         sc_Number;
  903.                     break;
  904.             }
  905.         }
  906.         mapMax = map > mapMax ? map : mapMax;
  907.     }
  908.     SC_Close();
  909.     MapCount = mapMax;
  910. }
  911.  
  912. //==========================================================================
  913. //
  914. // P_GetMapCluster
  915. //
  916. //==========================================================================
  917.  
  918. int P_GetMapCluster(int map)
  919. {
  920.     return MapInfo[QualifyMap(map)].cluster;
  921. }
  922.  
  923. //==========================================================================
  924. //
  925. // P_GetMapCDTrack
  926. //
  927. //==========================================================================
  928.  
  929. int P_GetMapCDTrack(int map)
  930. {
  931.     return MapInfo[QualifyMap(map)].cdTrack;
  932. }
  933.  
  934. //==========================================================================
  935. //
  936. // P_GetMapWarpTrans
  937. //
  938. //==========================================================================
  939.  
  940. int P_GetMapWarpTrans(int map)
  941. {
  942.     return MapInfo[QualifyMap(map)].warpTrans;
  943. }
  944.  
  945. //==========================================================================
  946. //
  947. // P_GetMapNextMap
  948. //
  949. //==========================================================================
  950.  
  951. int P_GetMapNextMap(int map)
  952. {
  953.     return MapInfo[QualifyMap(map)].nextMap;
  954. }
  955.  
  956. //==========================================================================
  957. //
  958. // P_TranslateMap
  959. //
  960. // Returns the actual map number given a warp map number.
  961. //
  962. //==========================================================================
  963.  
  964. int P_TranslateMap(int map)
  965. {
  966.     int i;
  967.  
  968.     for(i = 1; i < 99; i++) // Make this a macro
  969.     {
  970.         if(MapInfo[i].warpTrans == map)
  971.         {
  972.             return i;
  973.         }
  974.     }
  975.     // Not found
  976.     return -1;
  977. }
  978.  
  979. //==========================================================================
  980. //
  981. // P_GetMapSky1Texture
  982. //
  983. //==========================================================================
  984.  
  985. int P_GetMapSky1Texture(int map)
  986. {
  987.     return MapInfo[QualifyMap(map)].sky1Texture;
  988. }
  989.  
  990. //==========================================================================
  991. //
  992. // P_GetMapSky2Texture
  993. //
  994. //==========================================================================
  995.  
  996. int P_GetMapSky2Texture(int map)
  997. {
  998.     return MapInfo[QualifyMap(map)].sky2Texture;
  999. }
  1000.  
  1001. //==========================================================================
  1002. //
  1003. // P_GetMapName
  1004. //
  1005. //==========================================================================
  1006.  
  1007. char *P_GetMapName(int map)
  1008. {
  1009.     return MapInfo[QualifyMap(map)].name;
  1010. }
  1011.  
  1012. //==========================================================================
  1013. //
  1014. // P_GetMapSky1ScrollDelta
  1015. //
  1016. //==========================================================================
  1017.  
  1018. fixed_t P_GetMapSky1ScrollDelta(int map)
  1019. {
  1020.     return MapInfo[QualifyMap(map)].sky1ScrollDelta;
  1021. }
  1022.  
  1023. //==========================================================================
  1024. //
  1025. // P_GetMapSky2ScrollDelta
  1026. //
  1027. //==========================================================================
  1028.  
  1029. fixed_t P_GetMapSky2ScrollDelta(int map)
  1030. {
  1031.     return MapInfo[QualifyMap(map)].sky2ScrollDelta;
  1032. }
  1033.  
  1034. //==========================================================================
  1035. //
  1036. // P_GetMapDoubleSky
  1037. //
  1038. //==========================================================================
  1039.  
  1040. boolean P_GetMapDoubleSky(int map)
  1041. {
  1042.     return MapInfo[QualifyMap(map)].doubleSky;
  1043. }
  1044.  
  1045. //==========================================================================
  1046. //
  1047. // P_GetMapLightning
  1048. //
  1049. //==========================================================================
  1050.  
  1051. boolean P_GetMapLightning(int map)
  1052. {
  1053.     return MapInfo[QualifyMap(map)].lightning;
  1054. }
  1055.  
  1056. //==========================================================================
  1057. //
  1058. // P_GetMapFadeTable
  1059. //
  1060. //==========================================================================
  1061.  
  1062. boolean P_GetMapFadeTable(int map)
  1063. {
  1064.     return MapInfo[QualifyMap(map)].fadetable;
  1065. }
  1066.  
  1067. //==========================================================================
  1068. //
  1069. // P_GetMapSongLump
  1070. //
  1071. //==========================================================================
  1072.  
  1073. char *P_GetMapSongLump(int map)
  1074. {
  1075.     if(!strcasecmp(MapInfo[QualifyMap(map)].songLump, DEFAULT_SONG_LUMP))
  1076.     {
  1077.         return NULL;
  1078.     }
  1079.     else
  1080.     {
  1081.         return MapInfo[QualifyMap(map)].songLump;
  1082.     }
  1083. }
  1084.  
  1085. //==========================================================================
  1086. //
  1087. // P_PutMapSongLump
  1088. //
  1089. //==========================================================================
  1090.  
  1091. void P_PutMapSongLump(int map, char *lumpName)
  1092. {
  1093.     if(map < 1 || map > MapCount)
  1094.     {
  1095.         return;
  1096.     }
  1097.     strcpy(MapInfo[map].songLump, lumpName);
  1098. }
  1099.  
  1100. //==========================================================================
  1101. //
  1102. // P_GetCDStartTrack
  1103. //
  1104. //==========================================================================
  1105.  
  1106. int P_GetCDStartTrack(void)
  1107. {
  1108.     return cd_NonLevelTracks[MCMD_CD_STARTTRACK-MCMD_CD_STARTTRACK];
  1109. }
  1110.  
  1111. //==========================================================================
  1112. //
  1113. // P_GetCDEnd1Track
  1114. //
  1115. //==========================================================================
  1116.  
  1117. int P_GetCDEnd1Track(void)
  1118. {
  1119.     return cd_NonLevelTracks[MCMD_CD_END1TRACK-MCMD_CD_STARTTRACK];
  1120. }
  1121.  
  1122. //==========================================================================
  1123. //
  1124. // P_GetCDEnd2Track
  1125. //
  1126. //==========================================================================
  1127.  
  1128. int P_GetCDEnd2Track(void)
  1129. {
  1130.     return cd_NonLevelTracks[MCMD_CD_END2TRACK-MCMD_CD_STARTTRACK];
  1131. }
  1132.  
  1133. //==========================================================================
  1134. //
  1135. // P_GetCDEnd3Track
  1136. //
  1137. //==========================================================================
  1138.  
  1139. int P_GetCDEnd3Track(void)
  1140. {
  1141.     return cd_NonLevelTracks[MCMD_CD_END3TRACK-MCMD_CD_STARTTRACK];
  1142. }
  1143.  
  1144. //==========================================================================
  1145. //
  1146. // P_GetCDIntermissionTrack
  1147. //
  1148. //==========================================================================
  1149.  
  1150. int P_GetCDIntermissionTrack(void)
  1151. {
  1152.     return cd_NonLevelTracks[MCMD_CD_INTERTRACK-MCMD_CD_STARTTRACK];
  1153. }
  1154.  
  1155. //==========================================================================
  1156. //
  1157. // P_GetCDTitleTrack
  1158. //
  1159. //==========================================================================
  1160.  
  1161. int P_GetCDTitleTrack(void)
  1162. {
  1163.     return cd_NonLevelTracks[MCMD_CD_TITLETRACK-MCMD_CD_STARTTRACK];
  1164. }
  1165.  
  1166. //==========================================================================
  1167. //
  1168. // QualifyMap
  1169. //
  1170. //==========================================================================
  1171.  
  1172. static int QualifyMap(int map)
  1173. {
  1174.     return (map < 1 || map > MapCount) ? 0 : map;
  1175. }
  1176.  
  1177. //==========================================================================
  1178. //
  1179. // P_Init
  1180. //
  1181. //==========================================================================
  1182.  
  1183. void P_Init(void)
  1184. {
  1185.     InitMapInfo();
  1186.     P_InitSwitchList();
  1187.     P_InitFTAnims(); // Init flat and texture animations
  1188.     P_InitTerrainTypes();
  1189.     P_InitLava();
  1190.     R_InitSprites(sprnames);
  1191. }
  1192.  
  1193.  
  1194. // Special early initializer needed to start sound before R_Init()
  1195. void InitMapMusicInfo(void)
  1196. {
  1197.     int i;
  1198.  
  1199.     for (i=0; i<99; i++)
  1200.     {
  1201.         strcpy(MapInfo[i].songLump, DEFAULT_SONG_LUMP);
  1202.     }
  1203.     MapCount = 98;
  1204. }
  1205.  
  1206. /*
  1207. void My_Debug(void)
  1208. {
  1209.     int i;
  1210.  
  1211.     printf("My debug stuff ----------------------\n");
  1212.     printf("gamemap=%d\n",gamemap);
  1213.     for (i=0; i<10; i++)
  1214.     {
  1215.         printf("i=%d  songlump=%s\n",i,MapInfo[i].songLump);
  1216.     }
  1217. }
  1218. */
  1219.